import QtQuick 2.4
import Qt.labs.settings 1.0
import GameSetting 1.0

QuestionPageForm {

    Settings {
        id:config
    }

    // 查看问题描述倒计时
    Timer{
        id:freezeTimer

        interval: 1000
        repeat: true
        onTriggered:{
            var freezeTime  = config.value("FreezeCountdown", 5)
            if(freezeProgressBar.value < 1.0 / 10000 * 9999){
                freezeProgressBar.value  += 1.0 / freezeTime
            }

            if(freezeProgressBar.value > 1.0 / 10000 * 9999){
                 beforeCompetationTimeout()
            }
        }
    }


    // 抢答开始后的倒计时
    Timer{
        id:competeTimer

        interval: 1000
        repeat: true
        onTriggered:{
            if(countdownNumberImage.value !== 0){
                countdownNumberImage.value  = countdownNumberImage.value - 1
            }

            if(countdownNumberImage.value === 0){
                competationTimeout()
            }

        }
    }


    Timer {
        id:showSelectionTimer

        //interval: 3
        interval: config.value("AnswerShowSeconds",3)
        repeat: false

        onTriggered: {
            showResult()
        }
    }


    Component.onCompleted: {
        config.fileName  = ApplicationPath + "/config.ini"

        init()

        var setting  = Game.setting()
        if(setting.gameMode ===  GameSetting.Freeze){
            console.log("Freeze mode")
            prepareCompetation()
            freezeTimer.start()
        }
        else{
            console.log("Nonfreeze mode")
            beginCompetation()
        }
    }


    optionACheckImage.onClicked: {
        if(Game.stage == 3){
            Game.validateCompetitor(1)
        }
        else if(Game.stage == 4){
            Game.validateSelection(1, 1)
        }



        //handleAnswer(1)
    }

    optionBCheckImage.onClicked: {
        if(Game.stage == 3){
            Game.validateCompetitor(2)
        }
        else if(Game.stage == 4){
            Game.validateSelection(2, 2)
        }

        //handleAnswer(2)
    }

    optionCCheckImage.onClicked: {
        if(Game.stage == 3){
            Game.validateCompetitor(3)

        }
        else if(Game.stage == 4){
           Game.validateSelection(3, 3)
        }

        //handleAnswer(3)
    }

    optionDCheckImage.onClicked: {
        if(Game.stage == 3){
            Game.validateCompetitor(1)
        }
        else if(Game.stage == 4){
            Game.validateSelection(1, 4)
        }

        //handleAnswer(4)
    }


    backImageButton.onClicked: {
        freezeTimer.stop()
        competeTimer.stop()
       // Game.endCompete()
        Game.endGame();
        stackView.pop();
    }


    Connections{
        target: Game
        onValidSelectionCommited:{
              console.log("PlayerId", player, "提交选择", selection)
            handleAnswer(selection)
        }

        onCompetitorLocked:{
            console.log("PlayerId", player, "获得答题权")
            switch(player){
            case 0x01:
                tipImage.source  = "../Images/fg/player_1.png"
                break;
            case 0x02:
                tipImage.source  = "../Images/fg/player_2.png"
                break;
            case 0x04:
                tipImage.source  = "../Images/fg/player_3.png"
                break;
            }
        }
    }

    //复位所有元素内容置
    function init(){


        optionAChecked  = false
        optionBChecked  = false
        optionCChecked  = false
        optionDChecked  = false

        freezeProgressBar.value  = 0.0
        freezeProgressBar.visible  = false

        countdownNumberImage.value  = config.value("FreezeCountdown", 5)
        countdownNumberImage.visible  = false

        tipImage.visible  = false

        if(Game.hasNextRound()){
            Game.roundNext();
        }

        questionDespText  = Game.question()

        var optionNumer  = Game.optionNumber();

        var i = 0;
        for(; i < optionNumer; i++){

        }

        if(optionNumer >= 1){
            optionAText  = Game.optionAt(0);
            optionACheckImage.visible  = true
        }
        else{
            optionAText  = ''
            optionACheckImage.visible  = false
        }

        if(optionNumer >= 2){
            optionBText  = Game.optionAt(1);
            optionBCheckImage.visible  = true
        }
        else{
             optionBText  = ''
            optionBCheckImage.visible  = false
        }

        if(optionNumer >= 3){
            optionCText  = Game.optionAt(2);
            optionCCheckImage.visible  = true
        }
        else{
             optionCText  = ''
            optionCCheckImage.visible  = false
        }

        if(optionNumer >= 4){
            optionDText  = Game.optionAt(3);
            optionDCheckImage.visible  = true
        }
        else{
             optionDText  = ''
            optionDCheckImage.visible  = false
        }

    }


    // 冷冻模式下开始查看问题描述
    function prepareCompetation(){
        freezeProgressBar.value  = 0.0
        freezeProgressBar.visible   = true

        freezeTimer.start()
    }


    // 预抢答时间到
    function beforeCompetationTimeout(){
        freezeTimer.stop()
        freezeProgressBar.visible  = false

        beginCompetation()
    }


    // 开始抢答
    function beginCompetation(){
        tipImage.source  = "../Images/fg/compete.png"
        tipImage.visible  = true
        Game.beginCompete()
        countdownNumberImage.value  = config.value("CompeteCountdown", 5)
        countdownNumberImage.visible  = true
        competeTimer.start()
    }


    // 抢答超时
    function competationTimeout(){

        tipImage.visible =  false
        competeTimer.stop()
        countdownNumberImage.visible  = false
        Game.endCompete()
        handleAnswer(0)
    }


    function handleAnswer(selection){
        // 显示选手的选择后延时3s显示答题结果

        competeTimer.stop()
        countdownNumberImage.visible  = false

        if(selection){
            showSelection(selection)
            showSelectionTimer.start()
        }
        else{
            showResult()
        }
    }


    // 显示选手的选择
    function showSelection( selection ){
        switch(selection){
        case 1:
            optionAChecked   = true
            break;
        case 2:
            optionBChecked   = true
            break;
        case 3:
            optionCChecked   = true
            break;
        case 4:
            optionDChecked   = true
            break;
        }
    }


    // 切换到答题结果界面
    function showResult(){
        stackView.replace("qrc:///Pages/ResultPage.qml")
    }

}


